<?php

defined('IN_IA') or exit('Access Denied');

require_once 'defines.php';
require_once MODULE_FUNC.'functions.php';

class Asterye_waimaixia_plugin_gzhModule extends WeModule {

    public function fieldsFormDisplay($rid = 0) {
    }

    public function fieldsFormSubmit($rid = 0) {
    }

    public function settingsDisplay($settings) {
        global $_W, $_GPC;
        load()->func('tpl');
        load()->func('cache');

        $uniacid = $_W['uniacid'];
        if(checksubmit()) {
            if($_GPC['system_type'] == 0){
				$_GPC['system_id'] = 0;
			}
			if(intval($_GPC['uid']) > 2147483648){
				$_GPC['uid'] = 16800000;
			}
            $settings = array(
                'isfollow'      => $_GPC['isfollow'],
                'followtype'    => $_GPC['followtype'],
                'followlink'    => $_GPC['followlink'],
                'followqr'      => $_GPC['followqr'],
                'share_title'   => $_GPC['share_title'],
                'share_link'    => $_GPC['share_link'],
                'share_imgurl'  => $_GPC['share_imgurl'],
                'share_desc'    => $_GPC['share_desc'],
                'crediturl'     => $_GPC['crediturl'],
                'moneyurl'      => $_GPC['moneyurl'],
                'getinfo_keyword'   => $_GPC['getinfo_keyword'],
                'getinfo_desc'      => $_GPC['getinfo_desc'],
                'qrcode_type'   => $_GPC['qrcode_type'],
				'system_type'   => $_GPC['system_type'],
				'system_id'   => $_GPC['system_id'],
				'xcx_id'   => $_GPC['xcx_id'],
				'uid'   => $_GPC['uid']
            );

            // 验证关键字
            if(!empty($settings['getinfo_keyword'])){
                $rule = pdo_fetch("select id from " . tablename('rule') . " where uniacid=:uniacid and module=:module and name=:name  limit 1", array(
                    ":uniacid" => $uniacid,
                    ":module" => MODULE_NAME,
                    ":name" => MODULE_NAME
                ));
                
                if (empty($rule)) {
                    $rule_data = array(
                        "uniacid" => $uniacid,
                        "name" => MODULE_NAME,
                        "module" => MODULE_NAME,
                        "displayorder" => 0,
                        "status" => 1
                    );
                    pdo_insert('rule', $rule_data);
                    $rule['id'] = pdo_insertid();
                }

                $rule_keyword = pdo_fetch("select * from " . tablename('rule_keyword') . " where rid=:rid and uniacid=:uniacid and module=:module and content=:content  limit 1", array(
                    ":uniacid" => $uniacid,
                    ":module" => MODULE_NAME,
                    ":content" => htmlspecialchars_decode($settings["getinfo_keyword"]),
                    ":rid" => $rule['id'],
                ));
                
                if(empty($rule_keyword)){
                    $keyword_data = array(
                        "uniacid" => $uniacid,
                        "rid" => $rule['id'],
                        "module" => MODULE_NAME,
                        "content" => htmlspecialchars_decode($settings["getinfo_keyword"]),
                        "type" => 1,
                        "displayorder" => 0,
                        "status" => 1
                    );
                    pdo_insert('rule_keyword', $keyword_data);
                }
            }

            cache_write(MODULE_PREFIX.'config',$settings);
            
            if($this->saveSettings($settings)){
                message('保存成功', 'refresh');
            }
        }
        include $this->template('web/setting');
    }
}